package com.cy.pj.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


public class IPUtils {
    private static Logger logger =
            LoggerFactory.getLogger(IPUtils.class);
    public static String getIdAddr(){
        HttpServletRequest request=((ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes()).getRequest();
        String ip=null;
        try {
            request.getHeader("x-forwarded-for");
            if(StringUtil.isEmpty(ip)
            ||"unknown".equalsIgnoreCase(ip)){
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtil.isEmpty(ip)||
            ip.length()==0||"unknown".equalsIgnoreCase(ip)){
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtil.isEmpty(ip)||
                    "unknown".equalsIgnoreCase(ip)){
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtil.isEmpty(ip)||
                    "unknown".equalsIgnoreCase(ip)){
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtil.isEmpty(ip)||
                    "unknown".equalsIgnoreCase(ip)){
                ip = request.getRemoteAddr();
            }
        }catch (Exception e){
            logger.error("IPUtils ERROR ", e);
        }
        return  ip;
    }
}
